//
//  AppDelegate.m
//  Youhuiju
//
//  Created by yutenys on 16/7/13.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "AppDelegate.h"
#import "RootTabBarController.h"
#import <AFNetworking.h>
#import "UpdateView.h"
@interface AppDelegate ()

@property (strong, nonatomic) UpdateView *updateView;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
    
    if(version >= 8.0f){
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        
    }else{
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }
    
    [application registerForRemoteNotifications];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    
    [NSThread sleepForTimeInterval:1.5];
    
    [_window makeKeyAndVisible];
    self.window.rootViewController = [[RootTabBarController alloc]init];


    //版本更新检测
//    [self checkVersion];
    
    return YES;
}
//ios8新方法
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}

//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
//{
//    
//}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // 处理推送消息
    NSLog(@"userinfo:%@",userInfo);
    NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
    NSLog(@"Registfail%@",error);
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"deviceToken:%@",deviceToken);//这里的Token就是我们设备要告诉服务端的Token码
}

#pragma mark - 根视图控制器
//
//- (void)initRootViewController{
//    
//    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
//    
//    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:kShouldShowGuide];
//    
//    
//    if ([str isEqualToString:@"YES"]|| str == nil) { //空或者YES 显示
//        
//        SMGuideViewController *guide = [[SMGuideViewController alloc] init];
//        guide.isFromDelegate = YES;
//        
//        self.window.rootViewController = guide;
//        
//    }else{
//        self.window.rootViewController = [[RootTabBarController alloc]init];
//    }
//}

//版本检查
//- (void)checkVersion{
//    WeakObj(self);
//    [[AFHTTPSessionManager manager] GET:@"http://itunes.apple.com/lookup" parameters:@{@"id" : @"1100526210"} progress:nil success:^(NSURLSessionDataTask * task, id responseObject) {
//        
//        NSArray *result = [responseObject objectForKey:@"results"];
//        
//        if (result.count == 0) {
//            return ;
//        }
//        
//        NSString* version =[[[responseObject objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
//        
//        if ([version compare:kCurrentVersion] == NSOrderedDescending) {
//            
//            weakself.updateView.version = [[[responseObject objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
//            weakself.updateView.updateNote = [[[responseObject objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"];
//            weakself.updateView.urlString = [[[responseObject objectForKey:@"results"] objectAtIndex:0] valueForKey:@"trackViewUrl"];
//            
//            [weakself.updateView show];
//        }
//        
//    } failure:^(NSURLSessionDataTask * task, NSError * error) {
//        
//    }];
//}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

//版本更新提示视图
- (UpdateView *)updateView
{
    if (!_updateView) {
        _updateView = [[UpdateView alloc] initWithFrame:CGRectMake(0, 0, 235 * FitW, 391*FitH)];
        _updateView.center = self.window.center;
//        _updateView = [[UpdateView alloc]init];
            }
    return _updateView;
}

@end
